package com.yuyou.fn.timer.task;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.xxl.job.core.log.XxlJobLogger;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.ClassLecture;
import com.yuyou.fn.educational.service.IAttendClassLectureService;
import com.yuyou.fn.educational.service.IClassLectureService;
import com.yuyou.fn.educational.vo.AttendClassRecordUpdateVo;
import com.yuyou.fn.educational.vo.AttendClassRecordVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**0 00 23 L * ?
 * @Author: weiwenfeng
 * @Date: 2018/10/18
 */
@JobHander(value = "AutoAttendClassTask")
@Component
public class AutoAttendClassTask extends IJobHandler {

    @Resource
    private IAttendClassLectureService attendClassLectureService;

    @Resource
    private IClassLectureService classLectureService;

    @Override
    public ReturnT<String> execute(String... params) throws Exception {
        Date now = TimeUtils.nowDate();
        Date start = TimeUtils.firstDateOfMonth(now);
        Date end = TimeUtils.changeOneDateLastTime(TimeUtils.lastDateOfMonth(now));
        List<ClassLecture> classLectrues = classLectureService.findClassLectureBetweenDate(start, end, 0);
        //filterClassLecturesWithoutTeacher(classLectrues);
        for (ClassLecture classLecture : classLectrues) {
            Long classLectureId = classLecture.getClassLectureId();
            List<AttendClassRecordVo> recordDetailList =
                    classLectureService.findAttendClassRecordDetailByClassLectureId(classLectureId);
            List<AttendClassRecordUpdateVo> updateVos = new ArrayList<>(recordDetailList.size());
            for (AttendClassRecordVo attendClassRecordVo : recordDetailList) {
                AttendClassRecordUpdateVo updateVo = new AttendClassRecordUpdateVo();
                BeanUtils.copyProperties(updateVo, attendClassRecordVo);
                //没有预考勤默认为出勤
                if (attendClassRecordVo.getAttendanceStatus() == null) {
                    updateVo.setAttendanceStatus(0);
                }
                updateVos.add(updateVo);
            }
            // 完成考勤
            try {
                classLectureService.updateAttendClassRecord(classLectureId, updateVos, ClassLecture.AttendanceRoleEnum.系统.ordinal());
            }catch (Exception e){
                XxlJobLogger.log("classLectureId:"+classLectureId+"处理失败",e);
            }

        }
        return ReturnT.SUCCESS;
    }

    /**
     * 过滤掉没有老师的讲次
     *
     * @param classLectrues
     */
    private void filterClassLecturesWithoutTeacher(List<ClassLecture> classLectrues) {
        Iterator<ClassLecture> it = classLectrues.iterator();
        while (it.hasNext()) {
            ClassLecture classLecture = it.next();
            if (classLecture.getTeacherId() == null || classLecture.getTeacherId().equals("")) {
                it.remove();
            }
        }
    }
}
